﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Classwork09
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public static String GetWebContent(String Url, Encoding encoding)
        {
            try
            {
                if (Url.Equals("about:blank")) return null;
                if (!Url.StartsWith("http://") && !Url.StartsWith("https://")) Url = "http://" + Url;
                HttpWebRequest myreq = (HttpWebRequest)WebRequest.Create(Url);
                myreq.Timeout = 60000;
                myreq.KeepAlive = true;

                HttpWebResponse myres = (HttpWebResponse)myreq.GetResponse();

                Stream WebStream = myres.GetResponseStream();
                StreamReader SReader = new StreamReader(WebStream, encoding);
                String HtmlText = SReader.ReadToEnd();

                SReader.Close();
                WebStream.Close();
                return HtmlText;
            }
            catch(Exception ex)
            {
                MessageBox.Show("获取网站信息错误！" + ex.Message);
                return null;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string x = GetWebContent(textBox1.Text, Encoding.UTF8);
                Regex r1 = new Regex(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
                MatchCollection mc1 = r1.Matches(x);
                Regex r2 = new Regex("([1][0-9]{10})");
                MatchCollection mc2 = r2.Matches(x);

                for (int i = 0; i < mc1.Count; i++)
                {
                    if (mc1.Count == 0)
                    {
                        textBox2.Text = "未查询到邮箱地址！";
                    }
                    else
                    {
                        textBox2.Text += mc1[i].Value + "\r\n";
                    }
                }

                for(int i = 0; i < mc2.Count; i++)
                {
                    if (mc2.Count == 0)
                    {
                        textBox3.Text = "未查询到电话号码！";
                    }
                    else
                    {
                        textBox3.Text += mc2[i].Value + "\r\n";
                    }
                }
            }
            catch(Exception)
            {

            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string x = GetWebContent(textBox1.Text, Encoding.UTF8);
            textBox2.Text = x;
        }
    }
}
